package com.intouchtv.StartMenu;

import java.io.IOException;
import java.util.HashMap;

import com.intouchtv.ari.R;



import Servises.FilesManager;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class IconicAdapter extends ArrayAdapter<StartMenuEntry> {
		Activity context;
		static StartMenuEntry[] items;
		static View[] viewsCache;
		private HashMap<String, String> thumbsUrlToLocalPath;

		public IconicAdapter(Activity context, StartMenuEntry[] _items) {
			super(context, R.layout.list_entry_layout, _items);
			this.context=context;
			thumbsUrlToLocalPath = new HashMap<String, String>();
			items = _items;
			viewsCache = new View[items.length];
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			
			if (viewsCache[position] != null)
				return viewsCache[position];
			
			View row;
			LayoutInflater inflater=context.getLayoutInflater();
			try{
			row=inflater.inflate(R.layout.list_entry_layout,parent,false);
			}catch(Exception e)
			{
				return null;
			}
			
			TextView label=(TextView)row.findViewById(R.id.label);
			label.setText(items[position].title);
			
			TextView summary=(TextView)row.findViewById(R.id.summary);
			summary.setText(items[position].summary);
			
			ImageView thumb=(ImageView)row.findViewById(R.id.icon);
						
			try {
				thumbsUrlToLocalPath.put
					(items[position].imageUrl,
							FilesManager.SaveTempFileFromURL(items[position].imageUrl));
			} catch (IOException e) {
				return null;
			}
			
			Drawable draw  = Drawable.createFromPath(thumbsUrlToLocalPath.get(items[position].imageUrl));		
			thumb.setImageDrawable(draw);

			viewsCache[position] = row;
			
			return row;
		}
	}
